<template>
  <div class="app-container">
    <div class="title"><i class="el-icon-coin"></i>话题</div>
    <div class="filter-container">
      <el-select
        v-model="schoolId"
        placeholder="请选择所属院校"
        clearable
        @change="refresh_data()"
      >
        <el-option
          v-for="it in schoolList"
          :key="it.id"
          :label="it.name"
          :value="it.id"
        ></el-option>
      </el-select>
    </div>
    <el-table
      :data="tableData"
      v-loading="loadList"
      header-align="center"
      element-loading-text="拼命加载中..."
      style="width: 100%"
      border
    >
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="icon" label="图片">
        <template slot-scope="scope">
          <img :src="scope.row.icon" style="width: 50px; height: auto" />
        </template>
      </el-table-column>

      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="dialogShow(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <div v-show="!loadList" class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 50, 100, 200]"
        :page-size="pageSize"
        layout="total,prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div> -->

    <el-dialog title="修改备注" :visible.sync="dialogVisible" width="30%">
      <!-- <el-input
        type="textarea"
        :autosize="{ minRows: 6, maxRows: 12 }"
        placeholder="请输入产品须知"
        v-model.trim="item.remark"
      ></el-input> -->
      <el-form :model="item" ref="moForm" :rules="rules" label-width="130px">
        <el-form-item prop="name" label="名称">
          <el-input
            v-model.trim="item.name"
            placeholder="请输入名称"
          ></el-input>
        </el-form-item>

        <el-form-item prop="imageFile" label="图片">
          <el-button class="setimg" type="primary" @click="setImg"
            >选择图片</el-button
          >
          <br />
          <span class="imginfo">请选择.jpg/.png格式图片，大小不超过1M</span>
          <input
            type="file"
            accept=".jpg, .png"
            id="fileInput"
            @change="imgChange($event)"
          />
          <div class="imgbox">
            <img :src="iconImage" id="image" />
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { historyList, edit,getSchoolList } from "./api/index";
import { addImage ,filterSchool} from "../../../utils/common";
export default {
  data() {
    return {
      item: {},
      dialogVisible: false,
      loadList: true,
      tableData: [],
      schoolList: [],
      schoolId: window.sessionStorage.getItem("schoolId")
        ? window.sessionStorage.getItem("schoolId") * 1
        : "",
      reader: null,
      imgBase: "",
      iconImage: "",
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        imageFile: [{ required: true, message: "请选择图片", trigger: "blur" }],
      },
    };
  },
 async created() {
    this.reader = new FileReader();
    this.reader.onload = (e) => {
      this.imgBase = e.target.result;
    };
   await this.getSchoolList();
   await this.refresh_data();
    
  },
  methods: {
    setImg() {
      const target = document.getElementById("fileInput");
      target && target.click();
    },
    imgChange(e) {
      console.log(e);
      const img = e.target.files[0];
      if (img.size / 1024 < 2 * 1024) {
        this.reader.readAsDataURL(img);
        this.item.imageFile = img;
        setTimeout(() => {
          const pos = this.imgBase.indexOf("base64") + 7;
          this.imgBase = this.imgBase.substr(pos);
          this.iconImage = "data:image/png;base64," + this.imgBase;
        }, 500);
      } else {
        this.$message.error("图片大小不可超过2M");
      }
      this.$refs["moForm"].validateField("imageFile");
    },
    // 获取院校列表
    getSchoolList() {
      getSchoolList()
        .then((data) => {
          console.log(data, "=====");
          if (data.code === 1) {
            this.schoolList = filterSchool(data.data);
          } else {
            this.$message.error(data.msg);
          }
          this.loadList = false;
        })
        .catch((err) => {
          console.log(err);
          this.loadList = false;
        });
    },
    // 话题列表
    refresh_data() {
      window.sessionStorage.setItem("schoolId", this.schoolId);
      historyList(this.schoolId)
        .then((data) => {
          console.log(data, "=====");
          if (data.code === 1) {
            this.tableData = data.data;
          } else {
            this.$message.error(data.msg);
          }
          this.loadList = false;
        })
        .catch((err) => {
          console.log(err);
          this.loadList = false;
        });
    },
    handleSizeChange(val) {
      console.log(val, "111");
      this.pageSize = val;
      this.refresh_data(1, this.params);
    },
    handleCurrentChange(val) {
      console.log(val, "00000");
      this.refresh_data(val, this.params);
    },
    dialogShow(item) {
      this.item = { ...item, imageFile: "img_none" };
      this.iconImage = this.item.icon;
      this.dialogVisible = !this.dialogVisible;
    },
    // 修改备注
    async edit() {
      this.loadList = !this.loadList;
      if (
        this.item.imageFile &&
        this.item.imageFile !== "" &&
        this.item.imageFile !== "img_none"
      ) {
        this.item.icon = await addImage(this.item.imageFile);
      }
      delete this.item.imageFile;
      const res = await edit(this.item);
      if (res.code === 1) {
        this.refresh_data(this.currentPage);
        this.dialogVisible = !this.dialogVisible;
      } else {
        this.loadList = !this.loadList;
        this.$message.error(res.msg);
      }
    },
    // 置顶卡退款接口
    async cardRefund(item) {
      if (item.refundStatus == 0) {
        this.loadList = !this.loadList;
        const res = await cardRefund(item.id);
        if (res.code === 1) {
          this.refresh_data(this.currentPage);
        } else {
          this.loadList = !this.loadList;
          this.$message.error(res.msg);
        }
      }
    },
  },
};
</script>

<style scoped>
#fileInput {
  width: 0;
  height: 0;
  overflow: hidden;
}

.setimg {
  float: left;
  margin-left: 0;
}

.imgbox {
  border: 1px solid #eee;
  width: 200px;
  height: auto;
}

.imgbox img {
  align: center;
  max-width: 100%;
}

.imginfo {
  font-size: 12px;
  color: #666;
}
</style>
